import Vue from 'vue'
import Router from 'vue-router'
import ala from 'ala'
Vue.use(Router)
export const constantRouterMap = [
  {
    path: '/web',
    name: '官网首页',
    component: () => import('@/web/index'),
    children: [
      {
        path: 'home',
        name: '首页1',
        component: () => import('@/web/pages/home')
      },
      {
        path: 'index',
        name: '首页3',
        component: () => import('@/web/pages/home')
      },
      {
        path: 'download',
        name: '下载',
        component: () => import('@/web/pages/download')
      },
      {
        path: 'private',
        name: '隐私协议',
        component: () => import('@/web/pages/private')
      },
      {
        path: 'joinus',
        name: '加入我们',
        component: () => import('@/web/pages/joinus')
      },
      {
        path: 'wiki',
        component: () => import('@/pages/wiki')
      }
    ]
  },
  {
    path: '/web/pdf',
    name: 'pdf预览',
    component: () => import('@/web/pdf')
  },
  {
    path: '/web/reward',
    name: '分润维度',
    component: () => import('@/web/reward')
  },
  {
    path: '/web/standard',
    name: '首页2',
    component: () => import('@/standard/index'),
    children: [{
      path: 'index',
      name: '首页',
      component: () => import('@/standard/childrens/home')
    }]
  },
  {
    path: '/diy',
    component: () => import('@/pages/diy/default'),
    children: [
      {
        path: '',
        name: 'index_diy',
        meta: {
          title: 'DIY编辑',
          isDiy: true
        },
        component: () => import('@/pages/diy/index')
      }
    ]
  },
  {
    path: '/diy/jump',
    component: () => import('@/pages/diy/jump')
  },
  {
    path: '/admin/login',
    component: () => import('@/pages/admin/login'),
    meta: {
      themetype: 'admin'
    }
  },
  {
    path: '/admin/login/:sign',
    component: () => import('@/pages/admin/login'),
    meta: {
      themetype: 'admin'
    }
  },
  {
    path: '/admin-:themetype/login',
    component: () => import('@/pages/admin/login')
  },
  {
    path: '/admin-:themetype',
    component: () => import('@/admins/admin/admin-layout/index'),
    children: [
      {
        path: 'diy',
        name: '后台DIY系统',
        meta: {
          title: '后台diy编辑',
          isDiy: true
        },
        component: () => import('@/pages/diy/index')
      },
      {
        path: '*',
        component: () => import('@/pages/admin/index')
      }
    ]
  },
  {
    path: '/admin',
    component: () => import('@/admins/admin/admin-layout/index'),
    children: [
      {
        path: 'diy',
        name: 'admin_diy',
        meta: {
          title: '后台diy编辑',
          isDiy: true,
          filter: 3,
          themetype: 'admin'
        },
        params: {
          filter: 3
        },
        component: () => import('@/pages/diy/index')
      },
      {
        path: '*',
        meta: {
          title: '后台管理',
          filter: 3,
          themetype: 'admin'
        },
        params: {},
        component: () => import('@/pages/admin/index')
      }
    ]
  },
  {
    path: '/',
    component: () => import('@/pages/index'),
    name: 'index-root'
  },

  {
    path: '*',
    component: () => import('@/pages/index'),
    name: 'index-all'
  },
  {
    path: '/test',
    component: () => import('@/pages/test')
  },
  {
    path: '/401',
    component: () => import('@/pages/401')
  },
  {
    path: '/404',
    component: () => import('@/pages/404')
  }
]

const router = new Router({
  routes: constantRouterMap,
  mode: 'history',
  // mode: 'hash',
  scrollBehavior: () => ({
    y: 0
  })
})
router.beforeEach((to, from, next) => {
  if (to.name) {
    document.title = to.name
  }
  if (ala.tenantIs && ala.tenant() === null) {
    if (to.query.tenant === undefined) {
      ala.alert('访问路径不正确,请确保当前路径带有租户标识')
    } else {
      ala.localSet('tenant', to.query.tenant)
      next()
    }
  } else {
    next()
  }
})
export default router
